package com.htgd.gardenexpo.controller;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageInfo;
import com.htgd.gardenexpo.dto.GeConPlantDTO;
import com.htgd.gardenexpo.entity.CommonResult;
import com.htgd.gardenexpo.entity.GeConPlant;
import com.htgd.gardenexpo.entity.GeConPlantImg;
import com.htgd.gardenexpo.entity.GeConSport;
import com.htgd.gardenexpo.page.PlantQuery;
import com.htgd.gardenexpo.page.SportQuery;
import com.htgd.gardenexpo.service.GeConSportService;
import com.htgd.gardenexpo.service.WxUserCollectService;
import com.htgd.gardenexpo.utils.TokenUtil;
import com.htgd.gardenexpo.vo.GeConPlantVO;
import com.htgd.gardenexpo.vo.GeConSportVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 运动科普 前端控制器
 * </p>
 *
 * @author author
 * @since 2024-08-24
 */
@RestController
@RequestMapping("/api/sport")
@Api(tags = "运动科普")
public class GeConSportController {

    @Autowired
    private GeConSportService geConSportService;

    @Autowired
    private WxUserCollectService wxUserCollectService;


    @PostMapping(value = "/add")
    @ApiOperation("添加")
    @Transactional
    public CommonResult<JSONObject> add(@RequestBody GeConSport geConSport) {
        CommonResult<JSONObject> result = new CommonResult<>();
        try {
            geConSport.setStatus(4);
            geConSportService.save(geConSport);
            result.setCode(200);
            result.setMessage("添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(500);
        }
        return result;
    }

    @PostMapping(value = "/edit")
    @ApiOperation("编辑")
    public CommonResult<JSONObject> edit(@RequestBody GeConSport geConSport) {
        CommonResult<JSONObject> result = new CommonResult<>();
        try {
            geConSportService.updateById(geConSport);
            result.setCode(200);
            result.setMessage("修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(500);
        }
        return result;
    }

    @DeleteMapping(value = "/delete")
    @ApiOperation("删除")
    @Transactional
    public CommonResult<JSONObject> delete(String id) {
        CommonResult<JSONObject> result = new CommonResult<>();
        try {
            geConSportService.removeById(id);
            result.setCode(200);
            result.setMessage("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(500);
        }
        return result;
    }

    @GetMapping(value = "/list")
    @ApiOperation("查询")
    public CommonResult<List<GeConSportVO>> list(SportQuery sportQuery, HttpServletRequest request) {
        CommonResult<List<GeConSportVO>> result = new CommonResult<>();
        try {
            PageInfo<GeConSportVO> list = geConSportService.pageList(sportQuery,request);
            result.setCode(200);
            result.setMessage(String.valueOf(list.getTotal()));
            result.setData(list.getList());
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(500);
        }
        return result;
    }

    @GetMapping(value = "/details")
    @ApiOperation("详情")
    public synchronized CommonResult<GeConSport> details(String id, HttpServletRequest request) {
        CommonResult<GeConSport> result = new CommonResult<>();
        String accessToken = request.getHeader("Access_token");
        String openId = TokenUtil.getUsernameFromToken(accessToken);
        try {
            GeConSport geConSport = geConSportService.getById(id);
            geConSport.setHit(geConSport.getHit()+1);
            geConSportService.updateById(geConSport);
            Boolean collect = wxUserCollectService.isLike(openId, geConSport.getId(),"sport");
            if (collect) {
                geConSport.setIsLike(true);
            }else{
                geConSport.setIsLike(false);
            }
            result.setCode(200);
            result.setMessage("查询成功");
            result.setData(geConSport);
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(500);
        }
        return result;
    }

    @PostMapping(value = "/review")
    @ApiOperation("审核")
    public CommonResult<JSONObject> review(@RequestBody GeConSport geConSport) {
        CommonResult<JSONObject> result = new CommonResult<>();
        try {
            geConSportService.updateById(geConSport);
            result.setCode(200);
            result.setMessage("成功");
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(500);
        }
        return result;
    }

}
